use pest::error::{Error, InputLocation};
use crate::Rule;

/// 将 pest 错误打印为实验指定格式：
/// Error type B at Line X: ... (found `...`)
pub fn report_error(src: &str, e: Error<Rule>) {
    let (line, _col) = match e.line_col {
        pest::error::LineColLocation::Pos((l, c)) => (l, c),
        pest::error::LineColLocation::Span((l, c), _) => (l, c),
    };
    let offending = match e.location {
        InputLocation::Pos(pos) => src.get(pos..pos + 1).unwrap_or(""),
        InputLocation::Span((start, end)) => src.get(start..end).unwrap_or(""),
    };
    // 将期望集合压缩到单行，避免 pest 自带的多行箭头提示
    let expected = e.variant.message().replace('\n', " ");
    println!("Error type B at Line {}: {} (found `{}`)", line, expected, offending);
}
